package com.ddyx.common.framework.datasource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 动态数据源  路由配置
 * @author agula
 * @date 2019-07-16 17:58
 */
@Slf4j
public class DynamicDataSource extends AbstractRoutingDataSource
{
    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources)
    {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }

    @Override
    protected Object determineCurrentLookupKey()
    {
        String dataSourceName = DynamicDataSourceContextHolder.getDataSourceType();
        //log.info("当前数据源是：{}", dataSourceName);

        return DynamicDataSourceContextHolder.getDataSourceType();
    }
}